package com.woniuxy.enjoylife.controller;

import com.woniuxy.enjoylife.entity.Community;
import com.woniuxy.enjoylife.entity.House;
import com.woniuxy.enjoylife.enums.StateEnum;
import com.woniuxy.enjoylife.service.CommunityService;
import com.woniuxy.enjoylife.service.HouseService;
import com.woniuxy.enjoylife.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

@RestController
@RequestMapping("/house")
public class HouseContrller implements Serializable {
    @Autowired
    private CommunityService communityService;
    @Autowired
    private HouseService houseService;
    @Resource
    private HttpServletRequest request;

    /**
     * 新增房屋认证
     * @param id
     * @param community
     * @param building
     * @param unit
     * @param num
     * @return
     */
    @RequestMapping("/insertHouse")
    @ResponseBody
    public ResponseResult insertHouse(String id,String community, String building, String unit, String num){
        ResponseResult responseResult = new ResponseResult();
        //小区未被认证
        Community tempCommunity=communityService.isCertification(community);
        int cid=0;
        if(tempCommunity==null){
            responseResult.error();
            responseResult.setMessage("小区未被认证");
            return responseResult;
        }
        cid= tempCommunity.getId();
        System.out.println("id"+id+"cid"+cid+"community"+community+"building"+building+"unit"+unit+"num"+num);
        //房屋已被其他业主注认证
        House house = new House(Integer.parseInt(id),cid,Integer.parseInt(building),Integer.parseInt(unit),Integer.parseInt(num),false);
        if(houseService.isCertification(house)){
            responseResult.error();
            responseResult.setMessage("该房屋已被其他业主认证");
            return responseResult;
        }
        //新增认证房屋
        if(!houseService.insertHouse(house)){
            responseResult.error();
        }else{
            responseResult.success();
        }
        System.out.println("updateHouse");
        System.out.println(id+cid+community+building+unit+num);
        return responseResult;
    }

    /**
     * 通过用户和房屋id修改默认认证房屋
     * @param id
     * @return
     */
    @RequestMapping("/updateDefHouse")
    @ResponseBody
    public ResponseResult updateDefHouse(int id){
        int uid = (int) request.getSession().getAttribute("id");
        ResponseResult responseResult = new ResponseResult();

        if(houseService.updateDefHouse(id, uid)){
            responseResult.success();
        }else{
            responseResult.error();
        }
        return responseResult;
    }

    /**
     * 通过房屋id删除房屋
     * @param hid
     * @return
     */
    @RequestMapping("/delHouse")
    @ResponseBody
    public ResponseResult delHous(String hid){
        ResponseResult responseResult = new ResponseResult();
        System.out.println(hid);
        if(houseService.delHouse(Integer.parseInt(hid))){
            responseResult.success();
        }else {
            responseResult.error();
        }
        return responseResult;
    }

    /**
     * 修改房屋信息
     * @param cid
     * @param building
     * @param unit
     * @param num
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateHouse")
    public  ResponseResult updateHouse(String hid,String cid,String building, String unit, String num){
        ResponseResult responseResult = new ResponseResult();
        House house = new House(Integer.parseInt(hid),Integer.parseInt(cid),Integer.parseInt(building),Integer.parseInt(unit),Integer.parseInt(num));

        //查询房屋是否已被认证
        if(houseService.isCertification(house)){
            //true表示该房屋已被认证
            responseResult.error();
            responseResult.setMessage("该房屋已被其他业主认证");
            return  responseResult;
        }

        //房屋修改
        if(!houseService.updateHouse(house)){
            responseResult.error();
        }else{
            responseResult.success();
        }
        return responseResult;
    }



}
